package header

import "radar/nimrod/model"

// RayKey for Ray header
var RayKey = []string{
	"RayIndex",
	"AzimuthCentreAngle",
	"ElevationAngle",
	"NumOfBytesOfData",
	"NumOfRaysPerDegree",
}

// InitRayHeader inits the Ray header
func InitRayHeader() map[string][]byte {
	return map[string][]byte{
		RayKey[0]: make([]byte, 2),
		RayKey[1]: make([]byte, 2),
		RayKey[2]: make([]byte, 2),
		RayKey[3]: make([]byte, 2),
		RayKey[4]: make([]byte, 2),
	}
}

// Ray contains the slice of data element and the number of bins
type Ray struct {
	Header          map[string][]byte
	Data            []uint16
	Grids           []model.RadarGrid
	DataElementSize int16
	BinsNum         int16
	Elevation       float64
}

// isInsideGivenRegion judges grid is inside the given region or not
func (r *Ray) isInsideGivenRegion(min model.RadarGrid, max model.RadarGrid) []bool {
	isInside := make([]bool, len(r.Grids))
	for i, g := range r.Grids {
		if g.Lat >= min.Lat && g.Lon >= min.Lon && g.Lat <= max.Lat && g.Lon <= max.Lon {
			isInside[i] = true
		}
	}
	return isInside
}

// ExtractGivenRegion extracts the coordinates in the given region from raw polar file
func (r *Ray) ExtractGivenRegion(min model.RadarGrid, max model.RadarGrid) []model.RadarGrid {
	isInside := r.isInsideGivenRegion(min, max)
	grids := []model.RadarGrid{}
	for i, inside := range isInside {
		if inside {
			grids = append(grids, r.Grids[i])
		}
	}
	return grids
}

// ExtractData extracts the data in the given region from raw polar file
func (r *Ray) ExtractData(min model.RadarGrid, max model.RadarGrid) []uint16 {
	isInside := r.isInsideGivenRegion(min, max)
	data := []uint16{}
	for i, inside := range isInside {
		if inside {
			data = append(data, r.Data[i])
		}
	}
	return data
}
